package com.yifi.system.service;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseService;
import com.yifi.common.model.AjaxResult;
import com.yifi.common.utils.DateUtils;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.InvokeTask;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.common.vo.TableDataInfo;

/** 
* @ClassName: CronService 
* @Description: TODO
* @author 姚善利
* @date 2021-10-21 15:32:56 
*  
*/
public class CronService extends BaseService {

	/**
	 * 	动态添加定时任务
	 */
	public static void addCrons()
	{
		String sql="select * from sys_cron where cflag='1' order by isort";
		List<Record> list=Db.find(sql);
		for (Record record : list) {
			String cronid=record.getStr("id");	//任务ID
			String cronstr=record.getStr("cronstr"); //定时周期
			String jobClass=record.getStr("jobclass"); //定时类
			CronUtil.schedule(cronid,cronstr,new InvokeTask(jobClass));
		}
	}
	
	//测试任务方法
	public void demoJobClass()
	{
		System.out.println("执行方法"+DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", DateUtils.getNowDate()));
	}


	public TableDataInfo list(int pageNum, int pageSize, String cname)
	{
		String sqlSelect="select * ";
		String sqlExcept=" from sys_cron where 1=1 ";
		if(StrUtil.isNotEmpty(cname))
		{
			sqlExcept+=" and cname like '%"+cname+"%' ";
		}
		return dataPage(pageNum,pageSize,sqlSelect,sqlExcept,true);
	}

	public AjaxResult save(Controller controller)
	{
		String id=controller.getPara("id","");
		String cname=controller.getPara("cname","");
		String cronstr=controller.getPara("cronstr","");
		String cflag=controller.getPara("cflag","0");
		String jobclass=controller.getPara("jobclass","");
		String isort=controller.getPara("isort","1");

		Record record=getData("sys_cron","id",id);
		boolean isedit=true;
		if(record==null)
		{
			isedit=false;
			record=new Record();
			record.set("id", IdUtil.fastUUID());
			record.set("create_by", ToolSession.getUser(controller).getCname());
			record.set("create_time",DateUtils.getNowDate());
			record.set("cflag", cflag);
		}
		record.set("cname", cname);
		record.set("cronstr", cronstr);
		record.set("jobclass", jobclass);
		record.set("isort", isort);
		record.set("update_by", ToolSession.getUser(controller).getCname());
		record.set("update_time",DateUtils.getNowDate());
		if(DbUtils.save("sys_cron","id",record,isedit))
		{
			return success("保存成功！");
		}
		else
		{
			return error("保存失败！");
		}
	}

	public AjaxResult start(String id)
	{
		Record record=getData("sys_cron","id",id);
		record.set("cflag","1");
		record.set("update_by", "开启");
		record.set("update_time",DateUtils.getNowDate());
		if(Db.update("sys_cron","id",record))
		{
			String cronid=record.getStr("id");	//任务ID
			String cronstr=record.getStr("cronstr"); //定时周期
			String jobClass=record.getStr("jobclass"); //定时类
			CronUtil.schedule(cronid,cronstr,new InvokeTask(jobClass));
			return success("启动成功！");
		}
		else
		{
			return error("启动失败！");
		}
	}

	public AjaxResult stop(String id)
	{
		Record record=getData("sys_cron","id",id);
		record.set("cflag","0");
		record.set("update_by", "停止");
		record.set("update_time",DateUtils.getNowDate());
		if(Db.update("sys_cron","id",record))
		{
			String cronid=record.getStr("id");	//任务ID
			CronUtil.remove(cronid);
			return success("停止成功！");
		}
		else
		{
			return error("停止失败！");
		}
	}
}
